package logic

import (
	"context"
	"encoding/json"
	"models"
	"pkg"

	"vip_srv/internal/svc"
	"vip_srv/vip_srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderPayLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderPayLogic {
	return &OrderPayLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderPayLogic) OrderPay(in *vip_srv.OrderPayRequest) (*vip_srv.OrderPayResponse, error) {
	// todo: add your logic here and delete this line
	get, _ := models.RDB.Get(models.Ctx, in.Snow).Result()
	var order = models.Order{}
	json.Unmarshal([]byte(get), &order)
	pay, err := pkg.Pay(order)
	if err != nil {
		return nil, err
	}
	order.Status = "支付中"
	if err := models.DB.Create(&order).Error; err != nil {
		return nil, err
	}
	return &vip_srv.OrderPayResponse{
		Url: pay,
	}, nil
}
